package com.siao.applications
{
	import com.siao.data.HashMap;
	import com.siao.errors.SingletonError;
	
	import flash.display.Stage;
	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedClassName;
	
	/**
	 * 应用程序管理器
	 * @author SiaoLeon
	 */
	public class ApplicationManager
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:ApplicationManager;
		
		/**
		 * 获取单例对象
		 */
		public static function get instance():ApplicationManager
		{
			if (_instance == null)
			{
				_instance = new ApplicationManager();
			}
			return _instance;
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _stage:Stage;
		
		/**
		 * 当前应用程序所在舞台
		 * @return
		 */
		public function get stage():Stage
		{
			return _stage;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var applications:HashMap;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function ApplicationManager()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
			
			this.applications = new HashMap();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 获取应用程序加载参数
		 * @param name 参数名称
		 * @param value 默认值
		 * @return
		 */
		public function getParam(name:String, value:String=""):String
		{
			if (this.stage == null || this.stage.loaderInfo.parameters[name] == undefined || this.stage.loaderInfo.parameters[name] == null)
			{
				return value;
			}
			return this.stage.loaderInfo.parameters[name];
		}
		
		/**
		 * 根据类获取应用程序实例
		 * @param application 继承 Application 类的应用程序类
		 */
		public function getApplication(application:Object):Application
		{
			return this.applications.getValue(application);
		}
		
		/**
		 * 启动应用程序
		 * @param application 应用程序实例
		 * @throws Error
		 */
		public function startup(application:Application):void
		{
			var cls:Object = ApplicationDomain.currentDomain.getDefinition(getQualifiedClassName(application));
			this.applications.put(cls, application);
			
			if (this._stage == null && application.stage != null)
			{
				this._stage = application.stage;
				application.startup();
			}
			
			if (this.stage == null)
			{
				throw new Error("当有\"应用程序\"已添加到舞台后，才可启动其他\"应用程序\"");
			}
			
			if (application.stage == null)
			{
				application.startup();
			}
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
